package cn.tedu.service.petcommodity.Impl;

import cn.tedu.commons.response.JsonPage;
import cn.tedu.mapper.petcommodity.PetCommodityMapper;

import cn.tedu.pojo.pet.dto.PetCommodityDTO;
import cn.tedu.pojo.pet.entity.PetCategory;
import cn.tedu.pojo.pet.entity.PetCommodity;
import cn.tedu.pojo.pet.entity.PetDetails;
import cn.tedu.pojo.pet.vo.PetCommodityVO;
import cn.tedu.pojo.pet.vo.PetDetailsVO;

import cn.tedu.service.petcommodity.IPetCommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class PetCommodityImpl implements IPetCommodityService {
    @Autowired(required = false)
    private PetCommodityMapper petCommodityMapper;

    @Override
    public void addCommodity(PetCommodityDTO petCommodityDTO) {
        System.out.println(petCommodityDTO);
//        System.out.println(petCommodityDTO);
//        String commodityname= petCommodityDTO.getCommodityName();
//        PetCommodity queryResult= petCommodityMapper.getByCommodityName(commodityname);
//        if (queryResult!=null){
//            System.out.println("新增宠物商品失败");
//            throw new RuntimeException();
//        }

        PetCommodity petCommodity=new PetCommodity();
        petCommodity.setCommodityName(petCommodityDTO.getCommodityName());
        petCommodity.setCategoryId(petCommodityDTO.getCategoryId());
        System.out.println("获得的id为："+ petCommodityDTO.getCategoryId());
        petCommodity.setIsDelete(petCommodityDTO.getIsDelete());
        LocalDateTime now=LocalDateTime.now();
        petCommodity.setGmtCreate(now);
        petCommodity.setGmtModified(now);
        petCommodityMapper.addCommodity(petCommodity);
        System.out.println("添加宠物商品完成"+petCommodity);
    }

    @Override
    public void updatePetCommodity(String oldName,String newName) {
        PetCommodity queryResult= petCommodityMapper.getByCommodityName(newName);
        if (queryResult!=null){
            System.out.println("修改失败，此商品已存在");
            throw new RuntimeException();
        }
        PetCommodity petCommodity=new PetCommodity();
        petCommodity.setCommodityName(newName);
        System.out.println(oldName+newName);
        petCommodityMapper.updateCommodity(oldName,newName);
    }

    @Override
    public void updateIsDelete(String commodityName, Integer isDelete) {
        PetCommodity isExist= petCommodityMapper.getDetailsByCommodityName(commodityName);
        System.out.println("获取的名字是："+commodityName);
        System.out.println(petCommodityMapper.getDetailsByCommodityName(commodityName));
        if (isExist==null){
            System.out.println("指定的商品名不存在！");
            throw new RuntimeException("商品未找到");
        }
        petCommodityMapper.updateIsDelete(commodityName,isDelete);
        System.out.println("修改成功");

    }

    @Override
    public List<PetCommodityVO> listByCategoryName(String categoryName) {
        System.out.println("传入的名字为"+categoryName);
        System.out.println("查询结果为"+petCommodityMapper.getIdByCategoryName(categoryName));
        Long categoryId= petCommodityMapper.getIdByCategoryName(categoryName).getId();
        if (categoryId==null){
            System.out.println("错误！无此品种id");
            throw new RuntimeException("没找到所提供的品种id");
        }
        System.out.println(categoryId);

        System.out.println(petCommodityMapper.selectCommodity(categoryId));

        return petCommodityMapper.selectCommodity(categoryId);
    }

    @Override
    public PetDetailsVO listDetailsByCommodityName(String  commodityName) {
        System.out.println(commodityName);
        PetCommodity petCommodity=petCommodityMapper.getDetailsByCommodityName(commodityName);
        System.out.println(petCommodity);
        Long petDetails=petCommodity.getId();
        System.out.println(petDetails);
        if (petDetails==null){
            System.out.println("该商品未写入或商品id没找到");
            throw new RuntimeException("商品缺失");
        }
        System.out.println("得到的id为"+petDetails);
        System.out.println(petCommodityMapper.getDetailsByCommodityId(petDetails));
        PetDetails detailsExist=petCommodityMapper.getDetailsByCommodityId(petDetails);
        System.out.println("得到的信息为："+detailsExist);
        if (detailsExist==null){
            throw new RuntimeException("详情缺失");
        }
        petCommodityMapper.listDetailsById(petDetails);
        return petCommodityMapper.listDetailsById(petDetails);
    }


}
